{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Iterating through JSON\n",
    "\n",
    "This is a short tutorial on the difference between using pandas apply to read in complex JSON vs iterating directly through the JSON itself. More than a 100x improvement is seen by avoiding the `apply` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'id': 'EONET_4461',\n",
       " 'title': 'Hunter Fire',\n",
       " 'description': '',\n",
       " 'link': 'https://eonet.sci.gsfc.nasa.gov/api/v2.1/events/EONET_4461',\n",
       " 'categories': [{'id': 8, 'title': 'Wildfires'}],\n",
       " 'sources': [{'id': 'InciWeb',\n",
       "   'url': 'http://inciweb.nwcg.gov/incident/6645/'}],\n",
       " 'geometries': [{'date': '2019-10-14T11:30:00Z',\n",
       "   'type': 'Point',\n",
       "   'coordinates': [-116.097, 40.897]}]}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url = 'https://eonet.sci.gsfc.nasa.gov/api/v2.1/events?limit=10000'\n",
    "req = requests.get(url)\n",
    "json = req.json()\n",
    "events = json['events']\n",
    "events[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def use_pandas_apply(json):\n",
    "    df = pd.DataFrame(json)['events'].apply(pd.Series).set_index('id')\n",
    "    \n",
    "    df = df.categories.apply(pd.Series).stack().apply(pd.Series) \\\n",
    "           .reset_index(1, drop=True).title.rename('event').pipe(df.join)\n",
    "    del df['categories']\n",
    "    \n",
    "    df = df.geometries.apply(pd.Series).stack().apply(pd.Series) \\\n",
    "           .join(df).reset_index(-1, drop=True)\n",
    "    del df['geometries']\n",
    "    \n",
    "    df = df.coordinates.apply(pd.Series).rename(columns=dict(zip((0,1), \"xy\"))).join(df)\n",
    "    del df['coordinates']\n",
    "    \n",
    "    df['date'] = pd.to_datetime(df.date)\n",
    "    df = df[df.x.apply(lambda x: not isinstance(x, list))]\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def iterate_through_json(events):\n",
    "    event_dict = {'id': [], 'x': [], 'y': [], 'date': [], 'type': [], 'title': [],\n",
    "                  'description': [], 'link': [], 'sources': [], 'event': []}\n",
    "    for event in events:\n",
    "        for geometry in event['geometries']:\n",
    "            if len(geometry['coordinates']) == 2:\n",
    "                event_dict['date'].append(geometry['date'])\n",
    "                event_dict['x'].append(geometry['coordinates'][0])\n",
    "                event_dict['y'].append(geometry['coordinates'][1])\n",
    "                event_dict['type'].append(geometry['type'])\n",
    "                event_dict['id'].append(event['id'])\n",
    "                event_dict['title'].append(event['title'])\n",
    "                event_dict['description'].append(event['description'])\n",
    "                event_dict['link'].append(event['link'])\n",
    "                event_dict['event'].append(event['categories'][0]['title'])\n",
    "                event_dict['sources'].append(event['sources'])\n",
    "                \n",
    "    return pd.DataFrame(event_dict).set_index('id').sort_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>date</th>\n",
       "      <th>type</th>\n",
       "      <th>title</th>\n",
       "      <th>description</th>\n",
       "      <th>link</th>\n",
       "      <th>sources</th>\n",
       "      <th>event</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>EONET_2632</td>\n",
       "      <td>161.36</td>\n",
       "      <td>56.6530</td>\n",
       "      <td>2016-09-18 00:00:00+00:00</td>\n",
       "      <td>Point</td>\n",
       "      <td>Sheveluch Volcano, Russia</td>\n",
       "      <td>Sheveluch is one of the largest and most activ...</td>\n",
       "      <td>https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...</td>\n",
       "      <td>[{'id': 'EO', 'url': 'https://earthobservatory...</td>\n",
       "      <td>Volcanoes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>EONET_2654</td>\n",
       "      <td>-71.85</td>\n",
       "      <td>-15.7800</td>\n",
       "      <td>2016-11-07 00:00:00+00:00</td>\n",
       "      <td>Point</td>\n",
       "      <td>Sabancaya Volcano, Peru</td>\n",
       "      <td></td>\n",
       "      <td>https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...</td>\n",
       "      <td>[{'id': 'EO', 'url': 'https://earthobservatory...</td>\n",
       "      <td>Volcanoes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>EONET_2693</td>\n",
       "      <td>156.014</td>\n",
       "      <td>50.6860</td>\n",
       "      <td>2016-12-08 00:00:00+00:00</td>\n",
       "      <td>Point</td>\n",
       "      <td>Ebeko Volcano, Russia</td>\n",
       "      <td></td>\n",
       "      <td>https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...</td>\n",
       "      <td>[{'id': 'SIVolcano', 'url': 'http://volcano.si...</td>\n",
       "      <td>Volcanoes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>EONET_2734</td>\n",
       "      <td>-41.4727</td>\n",
       "      <td>-75.8853</td>\n",
       "      <td>2011-08-30 00:00:00+00:00</td>\n",
       "      <td>Point</td>\n",
       "      <td>Iceberg A23A</td>\n",
       "      <td></td>\n",
       "      <td>https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...</td>\n",
       "      <td>[{'id': 'BYU_ICE', 'url': 'http://www.scp.byu....</td>\n",
       "      <td>Sea and Lake Ice</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>EONET_2736</td>\n",
       "      <td>-108.526</td>\n",
       "      <td>-74.2006</td>\n",
       "      <td>2011-08-30 00:00:00+00:00</td>\n",
       "      <td>Point</td>\n",
       "      <td>Iceberg B22A</td>\n",
       "      <td></td>\n",
       "      <td>https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...</td>\n",
       "      <td>[{'id': 'BYU_ICE', 'url': 'http://www.scp.byu....</td>\n",
       "      <td>Sea and Lake Ice</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  x        y                      date   type  \\\n",
       "id                                                              \n",
       "EONET_2632   161.36  56.6530 2016-09-18 00:00:00+00:00  Point   \n",
       "EONET_2654   -71.85 -15.7800 2016-11-07 00:00:00+00:00  Point   \n",
       "EONET_2693  156.014  50.6860 2016-12-08 00:00:00+00:00  Point   \n",
       "EONET_2734 -41.4727 -75.8853 2011-08-30 00:00:00+00:00  Point   \n",
       "EONET_2736 -108.526 -74.2006 2011-08-30 00:00:00+00:00  Point   \n",
       "\n",
       "                                title  \\\n",
       "id                                      \n",
       "EONET_2632  Sheveluch Volcano, Russia   \n",
       "EONET_2654    Sabancaya Volcano, Peru   \n",
       "EONET_2693      Ebeko Volcano, Russia   \n",
       "EONET_2734               Iceberg A23A   \n",
       "EONET_2736               Iceberg B22A   \n",
       "\n",
       "                                                  description  \\\n",
       "id                                                              \n",
       "EONET_2632  Sheveluch is one of the largest and most activ...   \n",
       "EONET_2654                                                      \n",
       "EONET_2693                                                      \n",
       "EONET_2734                                                      \n",
       "EONET_2736                                                      \n",
       "\n",
       "                                                         link  \\\n",
       "id                                                              \n",
       "EONET_2632  https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...   \n",
       "EONET_2654  https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...   \n",
       "EONET_2693  https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...   \n",
       "EONET_2734  https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...   \n",
       "EONET_2736  https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...   \n",
       "\n",
       "                                                      sources  \\\n",
       "id                                                              \n",
       "EONET_2632  [{'id': 'EO', 'url': 'https://earthobservatory...   \n",
       "EONET_2654  [{'id': 'EO', 'url': 'https://earthobservatory...   \n",
       "EONET_2693  [{'id': 'SIVolcano', 'url': 'http://volcano.si...   \n",
       "EONET_2734  [{'id': 'BYU_ICE', 'url': 'http://www.scp.byu....   \n",
       "EONET_2736  [{'id': 'BYU_ICE', 'url': 'http://www.scp.byu....   \n",
       "\n",
       "                       event  \n",
       "id                            \n",
       "EONET_2632         Volcanoes  \n",
       "EONET_2654         Volcanoes  \n",
       "EONET_2693         Volcanoes  \n",
       "EONET_2734  Sea and Lake Ice  \n",
       "EONET_2736  Sea and Lake Ice  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "use_pandas_apply(json).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>date</th>\n",
       "      <th>type</th>\n",
       "      <th>title</th>\n",
       "      <th>description</th>\n",
       "      <th>link</th>\n",
       "      <th>sources</th>\n",
       "      <th>event</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>EONET_2632</td>\n",
       "      <td>161.3600</td>\n",
       "      <td>56.6530</td>\n",
       "      <td>2016-09-18T00:00:00Z</td>\n",
       "      <td>Point</td>\n",
       "      <td>Sheveluch Volcano, Russia</td>\n",
       "      <td>Sheveluch is one of the largest and most activ...</td>\n",
       "      <td>https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...</td>\n",
       "      <td>[{'id': 'EO', 'url': 'https://earthobservatory...</td>\n",
       "      <td>Volcanoes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>EONET_2654</td>\n",
       "      <td>-71.8500</td>\n",
       "      <td>-15.7800</td>\n",
       "      <td>2016-11-07T00:00:00Z</td>\n",
       "      <td>Point</td>\n",
       "      <td>Sabancaya Volcano, Peru</td>\n",
       "      <td></td>\n",
       "      <td>https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...</td>\n",
       "      <td>[{'id': 'EO', 'url': 'https://earthobservatory...</td>\n",
       "      <td>Volcanoes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>EONET_2693</td>\n",
       "      <td>156.0140</td>\n",
       "      <td>50.6860</td>\n",
       "      <td>2016-12-08T00:00:00Z</td>\n",
       "      <td>Point</td>\n",
       "      <td>Ebeko Volcano, Russia</td>\n",
       "      <td></td>\n",
       "      <td>https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...</td>\n",
       "      <td>[{'id': 'SIVolcano', 'url': 'http://volcano.si...</td>\n",
       "      <td>Volcanoes</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>EONET_2734</td>\n",
       "      <td>-41.4727</td>\n",
       "      <td>-75.8853</td>\n",
       "      <td>2011-08-30T00:00:00Z</td>\n",
       "      <td>Point</td>\n",
       "      <td>Iceberg A23A</td>\n",
       "      <td></td>\n",
       "      <td>https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...</td>\n",
       "      <td>[{'id': 'BYU_ICE', 'url': 'http://www.scp.byu....</td>\n",
       "      <td>Sea and Lake Ice</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>EONET_2736</td>\n",
       "      <td>-108.5265</td>\n",
       "      <td>-74.2006</td>\n",
       "      <td>2011-08-30T00:00:00Z</td>\n",
       "      <td>Point</td>\n",
       "      <td>Iceberg B22A</td>\n",
       "      <td></td>\n",
       "      <td>https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...</td>\n",
       "      <td>[{'id': 'BYU_ICE', 'url': 'http://www.scp.byu....</td>\n",
       "      <td>Sea and Lake Ice</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   x        y                  date   type  \\\n",
       "id                                                           \n",
       "EONET_2632  161.3600  56.6530  2016-09-18T00:00:00Z  Point   \n",
       "EONET_2654  -71.8500 -15.7800  2016-11-07T00:00:00Z  Point   \n",
       "EONET_2693  156.0140  50.6860  2016-12-08T00:00:00Z  Point   \n",
       "EONET_2734  -41.4727 -75.8853  2011-08-30T00:00:00Z  Point   \n",
       "EONET_2736 -108.5265 -74.2006  2011-08-30T00:00:00Z  Point   \n",
       "\n",
       "                                title  \\\n",
       "id                                      \n",
       "EONET_2632  Sheveluch Volcano, Russia   \n",
       "EONET_2654    Sabancaya Volcano, Peru   \n",
       "EONET_2693      Ebeko Volcano, Russia   \n",
       "EONET_2734               Iceberg A23A   \n",
       "EONET_2736               Iceberg B22A   \n",
       "\n",
       "                                                  description  \\\n",
       "id                                                              \n",
       "EONET_2632  Sheveluch is one of the largest and most activ...   \n",
       "EONET_2654                                                      \n",
       "EONET_2693                                                      \n",
       "EONET_2734                                                      \n",
       "EONET_2736                                                      \n",
       "\n",
       "                                                         link  \\\n",
       "id                                                              \n",
       "EONET_2632  https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...   \n",
       "EONET_2654  https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...   \n",
       "EONET_2693  https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...   \n",
       "EONET_2734  https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...   \n",
       "EONET_2736  https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...   \n",
       "\n",
       "                                                      sources  \\\n",
       "id                                                              \n",
       "EONET_2632  [{'id': 'EO', 'url': 'https://earthobservatory...   \n",
       "EONET_2654  [{'id': 'EO', 'url': 'https://earthobservatory...   \n",
       "EONET_2693  [{'id': 'SIVolcano', 'url': 'http://volcano.si...   \n",
       "EONET_2734  [{'id': 'BYU_ICE', 'url': 'http://www.scp.byu....   \n",
       "EONET_2736  [{'id': 'BYU_ICE', 'url': 'http://www.scp.byu....   \n",
       "\n",
       "                       event  \n",
       "id                            \n",
       "EONET_2632         Volcanoes  \n",
       "EONET_2654         Volcanoes  \n",
       "EONET_2693         Volcanoes  \n",
       "EONET_2734  Sea and Lake Ice  \n",
       "EONET_2736  Sea and Lake Ice  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iterate_through_json(events).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "476 ms ± 16.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "%timeit use_pandas_apply(json)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.53 ms ± 48.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit iterate_through_json(events)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pandas.io.json import json_normalize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def use_json_normalize(events):\n",
    "    df1 = json_normalize(events, 'categories', ['id', 'title', 'description', 'link', 'sources'], \n",
    "                         record_prefix='categories_').set_index('id').drop(columns=['categories_id']) \\\n",
    "                                                     .rename(columns={'categories_title': 'event'})\n",
    "    df2 = json_normalize(events, 'geometries', 'id').set_index('id')\n",
    "    df2['x'] = df2['coordinates'].str[0]\n",
    "    df2['y'] = df2['coordinates'].str[0]\n",
    "    df2 = df2.drop(columns='coordinates')\n",
    "    return df1.join(df2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>event</th>\n",
       "      <th>title</th>\n",
       "      <th>description</th>\n",
       "      <th>link</th>\n",
       "      <th>sources</th>\n",
       "      <th>date</th>\n",
       "      <th>type</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>EONET_2632</td>\n",
       "      <td>Volcanoes</td>\n",
       "      <td>Sheveluch Volcano, Russia</td>\n",
       "      <td>Sheveluch is one of the largest and most activ...</td>\n",
       "      <td>https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...</td>\n",
       "      <td>[{'id': 'EO', 'url': 'https://earthobservatory...</td>\n",
       "      <td>2016-09-18T00:00:00Z</td>\n",
       "      <td>Point</td>\n",
       "      <td>161.36</td>\n",
       "      <td>161.36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>EONET_2654</td>\n",
       "      <td>Volcanoes</td>\n",
       "      <td>Sabancaya Volcano, Peru</td>\n",
       "      <td></td>\n",
       "      <td>https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...</td>\n",
       "      <td>[{'id': 'EO', 'url': 'https://earthobservatory...</td>\n",
       "      <td>2016-11-07T00:00:00Z</td>\n",
       "      <td>Point</td>\n",
       "      <td>-71.85</td>\n",
       "      <td>-71.85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>EONET_2693</td>\n",
       "      <td>Volcanoes</td>\n",
       "      <td>Ebeko Volcano, Russia</td>\n",
       "      <td></td>\n",
       "      <td>https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...</td>\n",
       "      <td>[{'id': 'SIVolcano', 'url': 'http://volcano.si...</td>\n",
       "      <td>2016-12-08T00:00:00Z</td>\n",
       "      <td>Point</td>\n",
       "      <td>156.014</td>\n",
       "      <td>156.014</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>EONET_2734</td>\n",
       "      <td>Sea and Lake Ice</td>\n",
       "      <td>Iceberg A23A</td>\n",
       "      <td></td>\n",
       "      <td>https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...</td>\n",
       "      <td>[{'id': 'BYU_ICE', 'url': 'http://www.scp.byu....</td>\n",
       "      <td>2011-08-30T00:00:00Z</td>\n",
       "      <td>Point</td>\n",
       "      <td>-41.4727</td>\n",
       "      <td>-41.4727</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>EONET_2736</td>\n",
       "      <td>Sea and Lake Ice</td>\n",
       "      <td>Iceberg B22A</td>\n",
       "      <td></td>\n",
       "      <td>https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...</td>\n",
       "      <td>[{'id': 'BYU_ICE', 'url': 'http://www.scp.byu....</td>\n",
       "      <td>2011-08-30T00:00:00Z</td>\n",
       "      <td>Point</td>\n",
       "      <td>-108.526</td>\n",
       "      <td>-108.526</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       event                      title  \\\n",
       "id                                                        \n",
       "EONET_2632         Volcanoes  Sheveluch Volcano, Russia   \n",
       "EONET_2654         Volcanoes    Sabancaya Volcano, Peru   \n",
       "EONET_2693         Volcanoes      Ebeko Volcano, Russia   \n",
       "EONET_2734  Sea and Lake Ice               Iceberg A23A   \n",
       "EONET_2736  Sea and Lake Ice               Iceberg B22A   \n",
       "\n",
       "                                                  description  \\\n",
       "id                                                              \n",
       "EONET_2632  Sheveluch is one of the largest and most activ...   \n",
       "EONET_2654                                                      \n",
       "EONET_2693                                                      \n",
       "EONET_2734                                                      \n",
       "EONET_2736                                                      \n",
       "\n",
       "                                                         link  \\\n",
       "id                                                              \n",
       "EONET_2632  https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...   \n",
       "EONET_2654  https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...   \n",
       "EONET_2693  https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...   \n",
       "EONET_2734  https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...   \n",
       "EONET_2736  https://eonet.sci.gsfc.nasa.gov/api/v2.1/event...   \n",
       "\n",
       "                                                      sources  \\\n",
       "id                                                              \n",
       "EONET_2632  [{'id': 'EO', 'url': 'https://earthobservatory...   \n",
       "EONET_2654  [{'id': 'EO', 'url': 'https://earthobservatory...   \n",
       "EONET_2693  [{'id': 'SIVolcano', 'url': 'http://volcano.si...   \n",
       "EONET_2734  [{'id': 'BYU_ICE', 'url': 'http://www.scp.byu....   \n",
       "EONET_2736  [{'id': 'BYU_ICE', 'url': 'http://www.scp.byu....   \n",
       "\n",
       "                            date   type        x        y  \n",
       "id                                                         \n",
       "EONET_2632  2016-09-18T00:00:00Z  Point   161.36   161.36  \n",
       "EONET_2654  2016-11-07T00:00:00Z  Point   -71.85   -71.85  \n",
       "EONET_2693  2016-12-08T00:00:00Z  Point  156.014  156.014  \n",
       "EONET_2734  2011-08-30T00:00:00Z  Point -41.4727 -41.4727  \n",
       "EONET_2736  2011-08-30T00:00:00Z  Point -108.526 -108.526  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "use_json_normalize(events).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19.5 ms ± 496 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit use_json_normalize(events)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
